from nicegui import ui
import  uuid 
import shelve
from config import database_path
from fastapi.responses import RedirectResponse

def navigation():
    with ui.header().classes(replace='row items-center') as header:
        ui.button('首页',on_click=lambda: ui.open('/'))
        ui.button('配置解析',on_click=lambda: ui.open('text-fsm'))
        ui.button('EXCEL自动化',on_click=lambda: ui.open('excel-sql'))
        ui.button('流程自动化',on_click=lambda: ui.open('process'))

    
    with ui.footer(value=True) as footer:
        with ui.row().classes('gap-24'):
            ui.label('版权所有:段福').classes('text-left')
            ui.label('联系方式:duanfu456@163.com').classes('text-right')
            ui.label('版本:1.0.0').classes('text-left')


def authentication():
    with shelve.open(database_path) as db:
        auth=db['authentication']
    if auth['esn']==uuid.getnode() and  auth['status']:
        return True
    else:
        ui.label('认证失败！！！')
        ui.label('请认证后使用')
        ui.button('返回认证',on_click=lambda: ui.open('/'))
        

class DATA:
    def __init__(self,datas:list[dict],key) -> None:
        self.datas=datas
        self.key=key
    def result(self):
        return self.datas
    def delete(self, key):
        for index , data in  enumerate (self.datas):
            if data[self.key]==key:
                self.datas.remove(data)
                return self.datas
    
    def update(self,newdata:dict):
        key=newdata.get(self.key,None)
        if key:
            for index, data in  enumerate (self.datas):
                if data[self.key]==key:
                    self.datas[index]=newdata
                    return self.datas
            # 新的数据
            self.datas.append(newdata)
            return self.datas
            
        else:
            return Exception('key is not exist')
        
    def add(self,newdata:dict):
        key=newdata.get(self.key,None)
        if key:
            for index, data in  enumerate (self.datas):
                if data[self.key]==key:
                    self.datas[index]=newdata
                    return self.datas
            # 新的数据
            self.datas.append(newdata)
            return self.datas
        else:
            return Exception('key is not exist')
        
    def find_keys(self):
        keys=[]
        for index, data in  enumerate (self.datas):
            keys.append(data[self.key])
        return list(set(keys))
    def find(self,key):
        for index, data in  enumerate (self.datas):
            if data[self.key]==key:
                return data
    